import axios from 'axios'
import Cookies from 'js-cookie'



axios.defaults.baseURL='http://127.0.0.1:8000/';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
if (Cookies.get('token')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
  axios.defaults.headers.common['Authorization'] = `JWT ${Cookies.get('token')}`;
}



//发布文章
export const sendArticle= (parms) => {
  return new Promise((resolve, reject) => {
    axios.post('/api/article/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};

//删除文章
export const deleteArticle= (parms) => {
  return new Promise((resolve, reject) => {
    axios.delete('/api/article/' + parms + '/').then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//拉取个人的文章
export const getArticle= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/article/' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//拉取所有文章
export const getArticleAll= (id,parms) => {
  if(id===''){
    return new Promise((resolve, reject) => {
      axios.get('/api/articleAll/' + parms).then(res=>{
        resolve(res)})
        .catch(error=>{reject(error)});
    })
  }else{
    return new Promise((resolve, reject) => {
      axios.get('/api/articleAll/' + id + '/' + parms).then(res=>{
        resolve(res)})
        .catch(error=>{reject(error)});
    })
  }

};


//拉取指定用户的文章
export const getSpecialArticle= (id,parms) => {
  if (parms===''){
    return new Promise((resolve, reject) => {
      axios.get('/api/articleAll/?user=' + id,parms).then(res=>{
        resolve(res)})
        .catch(error=>{reject(error)});
    })
  } else{
    return new Promise((resolve, reject) => {
      axios.get('/api/articleAll/?user=' + id + '&' + parms).then(res=>{
        resolve(res)})
        .catch(error=>{reject(error)});
    })
  }
};


//拉取热搜关键词
export const getSearchHot= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/searchHot/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};




//搜索文章
export const getSearchArticle= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/articleAll/?search=' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//发送相册图片
export const postAlbum= (parms) => {
  return new Promise((resolve, reject) => {
    axios.post('/api/album/',parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};


//拉取个人相册图片
export const getAlbum= (parms) => {
  return new Promise((resolve, reject) => {
    axios.get('/api/album/' + parms).then(res=>{
      resolve(res)})
      .catch(error=>{reject(error)});
  })
};
